﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace RilattackLibrary
{
    public class Scene
    {
		private static Scene _instance;
        public static float Gravity
        {
            get; set;
        }

        public static Scene Instance
        {
            get
            {
                if(_instance == null)
                {
					Console.WriteLine("Instantiating new scene");
                    _instance = new Scene();
                }
                return _instance;
            }
        }


        public Dictionary<string, Texture2D> Textures = new Dictionary<string, Texture2D>();

        public List<GraphicObject> Actors
        {
            get; set;
        }

        public GameConfiguration GameConfiguration
        {
            get; set;
        }

        public void Initialize()
        {
            Actors = new List<GraphicObject>();
            Actors = Actors.Concat(Map.Instance.CreateMapElements(Textures["tiles1"], 16, 16, 1.0)).ToList();
            Gravity = 1.1f;
		}

		public void AddNewPlayer(Player p)
		{
            Console.WriteLine("Adding new player "+ p.Nickname);

            p.Image = Textures["Worms/wblink1"];
			p.Source = new Rectangle(Player.posXinSprite, Player.posYinSprite, Player.playerW, Player.playerH);
			//p.Dest = new Rectangle(64, 64, 60, 60);
			Actors.Add(p);
		}

		public bool SetPlayerPos(String nick, int x, int y)
		{
            if (Actors != null)
			{
                lock (Actors) {
                    foreach (Actor a in Actors)
                    {
						Player p = a as Player;
						if (p !=null && p.Nickname == nick)
                        {
                            int px;
                            int py;
                            p.GetPosition(out px,out py);
                            p.SetPosition(x, y);
                            return true;
                        }
                    }
                }
			}

			return false;
		}

        public void Act(SpriteBatch spriteBatch)
        {
            lock (Actors)
            {
                foreach (Actor actor in Actors)
                {
                    actor.Play(spriteBatch);
                }
            }
        }
    }
}